package com.mmall.example.syncContainer.vector;

import com.mmall.annoations.NotThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.util.Vector;


/**
 * Vector 同步容器 - > 在某些情况下会出现线程不安全的现象
 * remove 和 get 操作顺序混乱的时候会发生线程不安全
 * Created by megan on 2018/4/2.
 */
@Slf4j
@NotThreadSafe
public class VectorExample2 {

    private static Vector<Integer> vector = new Vector<>();

    public static void main(String[] args) {
        while (true){
            for(int i = 0,b=10; i < b; i++){
                /** 初始化vector 的值 **/
                vector.add(i);
            }

            Thread thread1  =  new Thread(){
                public void run(){
                   for(int i = 0, b = vector.size(); i < b; i++){
                       vector.remove(i);
                   }
                }
            };

            Thread thread2  =  new Thread(){
                public void run(){
                    for(int i = 0,b = vector.size(); i < b; i++){
                        vector.get(i);
                    }
                }
            };
            thread1.start();
            thread2.start();
        }
    }

}
